/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.client;

import org.springframework.richclient.application.ApplicationWindow;
import org.springframework.richclient.application.config.ApplicationWindowConfigurer;
import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
import org.springframework.richclient.application.setup.SetupWizard;
import org.springframework.util.Assert;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * Custom application lifecycle implementation that configures the jmplayer app
 * at well defined points within its lifecycle.
 */
public class JmplayerLifecycleAdvisor extends DefaultApplicationLifecycleAdvisor {

    /**
     * File to be made when the wizard is runned to prevent it from running every time
     * TODO: Use RCP preferences if possible at this
     */
    private String wizardRunFile;

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(wizardRunFile, "A wizardRunFile is required");
        super.afterPropertiesSet();
    }

    public void onPreWindowOpen(ApplicationWindowConfigurer configurer) {
        super.onPreWindowOpen(configurer);

        if (getApplication().getApplicationContext().containsBean("setupWizard")) {
            File file = new File(wizardRunFile);
            if (!file.exists()) {
                SetupWizard setupWizard = (SetupWizard) getApplication().getApplicationContext().getBean("setupWizard", SetupWizard.class);
                setupWizard.execute();
                if (setupWizard.onFinish()) {
                    file.getParentFile().mkdirs();
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        // comment out to hide the menubar, toolbar, or reduce window size...
        //configurer.setShowMenuBar(false);
        //configurer.setShowToolBar(false);
        //configurer.setInitialSize(new Dimension(640, 480));
    }

    /**
     * Called when the window is being closed. This hook allows control over
     * whether the window is allowed to close. By returning false from this
     * method, the window will not be closed.
     *
     * @return boolean indicator if window should be closed. <code>true</code>
     *         to allow the close, <code>false</code> to prevent the close.
     */
    public boolean onPreWindowClose(ApplicationWindow window) {
        // TODO: Use resource bundle
        return JOptionPane.showConfirmDialog(window.getControl(),
                "Are you sure you want to exit the application?",
                "Confirm Exit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    }

    public void setWizardRunFile(String wizardRunFile) {
        this.wizardRunFile = wizardRunFile;
    }

}